package edu.ensmp.ckEditor.control;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import edu.ensmp.ckEditor.ckModel.CKModel;
import edu.ensmp.ckEditor.mousePlugin.EditingGraphMousePlugin;
import edu.ensmp.ckEditor.util.StringMethods;

public class EditorFrameListener implements WindowListener{
	
	private JFrame frame;
	private CKModel model;

	public EditorFrameListener(CKModel model, JFrame frame) {
		this.frame = frame;
		this.model = model;
	}

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent e) {
		if (EditingGraphMousePlugin.save) {
			System.exit(0);
		} else if (!EditingGraphMousePlugin.save) {
			
			//Custom button text
			Object[] options = {"Yes","No","Cancel"};
			int n = JOptionPane.showConfirmDialog(frame, "Do you want to save changes before exiting?", "Unsaved changes", JOptionPane.YES_NO_CANCEL_OPTION);
			System.out.println(n);
			
			switch(n){

			case 0:
				
				/**
				* Fonction qui permet la sauvegarde des vertex et de leurs coordonées
				* pour ensuite etre reconstruit par la suite
				*/
				JFileChooser fc = new JFileChooser();
				File file = new File("default.eck");
				int returnVal = fc.showSaveDialog(fc);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
			            file = fc.getSelectedFile();
						EditingGraphMousePlugin.save = true;
			            if(!file.getName().toLowerCase().endsWith(".eck")){
			            	String filePath = file.getPath().concat(".eck");
			            	file = new File(filePath);
			            } else {
			            	file = new File(file.getPath());
			            }
			         
					
					try {
						// On instancie le flot d'ecriture
						FileWriter fw = new FileWriter(file);
						/*
						 * A cet emplacement on écrira tous les concepts
						 */
						fw.write("DebutConcept"+ System.getProperty("line.separator"));
						fw.write(CKModel.vecConceptSave.size() + System.getProperty("line.separator") + System.getProperty("line.separator"));
						for ( int i = 0; i < CKModel.vecConceptSave.size(); i++){
							fw.write(CKModel.vecConceptSave.elementAt(i).getIndex() + System.getProperty("line.separator"));
							fw.write(StringMethods.forHTML(CKModel.vecConceptSave.elementAt(i).getProposition()) +System.getProperty("line.separator"));
							fw.write(CKModel.vecConceptSave.elementAt(i).getPositionX() + System.getProperty("line.separator"));
							fw.write(CKModel.vecConceptSave.elementAt(i).getPositionY() + System.getProperty("line.separator") + System.getProperty("line.separator"));
						}
						
						/*
						 * A cet emplacement on écrira tous les Knowledges
						 */
						fw.write("DebutKnowledge"+ System.getProperty("line.separator"));
						fw.write(CKModel.vecKnowledgeSave.size() + System.getProperty("line.separator") + System.getProperty("line.separator"));
						for ( int i = 0; i < CKModel.vecKnowledgeSave.size(); i++){
							fw.write(CKModel.vecKnowledgeSave.elementAt(i).getIndex() + System.getProperty("line.separator"));
							fw.write(StringMethods.forHTML(CKModel.vecKnowledgeSave.elementAt(i).getProposition()) +System.getProperty("line.separator"));
							fw.write(CKModel.vecKnowledgeSave.elementAt(i).getPositionX() + System.getProperty("line.separator"));
							fw.write(CKModel.vecKnowledgeSave.elementAt(i).getPositionY() + System.getProperty("line.separator") + System.getProperty("line.separator"));
						}
						
						fw.write("DebutEdge"+ System.getProperty("line.separator"));
						fw.write(CKModel.vecEdgeSave.size() + System.getProperty("line.separator") + System.getProperty("line.separator"));
						for ( int i = 0; i < CKModel.vecEdgeSave.size(); i++){
							fw.write(CKModel.vecEdgeSave.elementAt(i).getIndex() + System.getProperty("line.separator"));
							fw.write(CKModel.vecEdgeSave.elementAt(i).getIndiceDebut() + System.getProperty("line.separator"));
							fw.write(CKModel.vecEdgeSave.elementAt(i).getIndiceFin() + System.getProperty("line.separator") + System.getProperty("line.separator"));
						}
						fw.close();
						
					} catch (IOException exception) {

						exception.printStackTrace();
					}	
				}
				
			case 1:
				System.exit(0);
			}
		}
		
	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

}
